package com.erp.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.appengine.api.fetchurl.FetchUrlService;
import com.alibaba.appengine.api.fetchurl.FetchUrlServiceFactory;

/**
 * Simple demo of {@link com.alibaba.appengine.api.fetchurl.FetchUrlService}
 * 
 * @author ding.lid
 */
public class FetchUrlServiceDemo extends HttpServlet {
	private static final long serialVersionUID = -2359366324351384788L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.getWriter().println("Begin fetchurl service test.");
		FetchUrlService fetchUrlService = FetchUrlServiceFactory
				.getFetchUrlService();

		final String url = "http://www.taobao.com";
		String body = fetchUrlService.get(url);
		response.getWriter().printf("Fetch text from url %s: %s...\n", url,
				body.substring(0, 256));
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
